// pages/assigned /assigned .js
var app = getApp();
var api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    numIdx: '',
    outnumber: ['小于100', '100-300', '300-600', '大于600'],
    agree: true,
    imgArr: [],
    modalShow: true,
    second: 59,
  },

  //入驻协议
  modalShow() {
    this.setData({
      modalShow: false
    })
  },
  //关闭弹窗
  closeModal() {
    this.setData({
      modalShow: true
    })
  },

  //机构人数选择
  numSelect(e) {
    this.setData({
      numIdx: e.detail.value
    })
  },

  agreeOn(e) {
    this.setData({
      agree: !this.data.agree
    })
  },

  //图片上传
  unploadImg() {
    wx.chooseImage({
      count: 1,
      success: (res) => {
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        // var imgArr = [...this.data.imgArr, ...tempFilePaths];
        // this.setData({
        //   imgArr
        // })
        wx.uploadFile({
          url: app.globalData.host + 'Settled/upload_img',
          filePath: tempFilePaths[0],
          name: 'file',
          success: (req) => {

            var data = JSON.parse(req.data)
            console.log(data.msg)
            if (data.code == 2000) {
              this.data.imgArr.push(data.data.set_path)
              console.log(data.data.set_path)
              this.setData({
                imgArr: this.data.imgArr
              })
            } else {
              wx.showToast({
                title: '图片上传失败',
                icon: 'none'
              })
            }
          }
        })
      }
    })
  },
  //图片预览
  previewImg(e) {
    var imgs = e.currentTarget.dataset.imgs,
      img = e.currentTarget.dataset.img;
    console.log(imgs, img)
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: imgs // 需要预览的图片http链接列表
    })
  },

  //提交
  formSubmit(e) {
    var params = e.detail.value
    if (!params.name) {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return
    }
    // if (!params.e_mail) {
    //   wx.showToast({
    //     title: '请输入邮箱',
    //     icon: 'none'
    //   })
    //   return
    // }
    if (!params.password) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
      return
    }
    if (!params.phone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return
    }
      if (!params.code) {
        wx.showToast({
          title: '请输入验证码',
          icon: 'none'
        })
      return
    }

    var imgArr = {
      "license": this.data.imgArr[0]
    }
    params = {
      ...params,
      ...imgArr
    }
    console.log(params)
    api.fetchApi('Settled/register', params).then(res => {
      console.log(res)
      wx.showToast({
        title: '提交成功',
      })
      setTimeout(() => {
        wx.switchTab({
          url: '/pages/personal/personal'
        })
      }, 1000)
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  getCode() {
    if (!this.data.phoneNumber) {
      wx.showToast({
        title: '请填写手机号码',
        icon: 'none'
      })
      return
    }
    api.fetchApi('settled/send_sms', {
      phone_number: this.data.phoneNumber
    }).then(res => {
      api.showToast('发送成功')
      this.setData({
        isClick: true
      })

      this.data.timer = setInterval(() => {
        var second = this.data.second;
        second--
        if (second <= 1) {
          second = 59;
          this.setData({
            isClick: false
          })
          clearInterval(this.data.timer)
        }
        this.setData({
          second
        })
      }, 1000)
    })
  },
  //获取验证码
  getYzm() {
    this.getCode()
  },
  phoneNumber(e) {
    // console.log(e.detail.value)
    this.setData({
      phoneNumber: e.detail.value
    })
  },
})